<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>微博首页</h1>
    <div v-if="user==null">
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-else>
        <h2>欢迎{{user.nick}}回来</h2>
        <a href="/send.html">发微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
                {{weibo.nick}}:{{weibo.content}}
            </a>
        </h3>
        <img v-for="url in weibo.urlsArr" :src="url" width="100">
    </div>
</div>
<!--引入Vue框架-->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            user:null,
            arr:[]
        },
        methods:{
            logout(){
                if (confirm("您确定退出登录吗?")){
                    //发出退出登录的请求
                    axios.get("/logout").then(function () {
                        v.user=null;
                    })
                }

            }
        },
        created:function () {
            //发请求得到当前登录的用户对象
            axios.get("/currentUser").then(function (response) {
                //如果没有登录得到的是空字符串
                v.user = response.data==""?null:response.data;
            })
            //发出获取所有微博的请求
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data;
                for (let weibo of v.arr) {
                    //把weibo对象中表示多个图片路径的字符串通过,拆分得到一个数组
                    //把表示图片路径的数组再动态的给weibo对象添加一个属性保存
                    weibo.urlsArr = weibo.urls.split(",");
                }



            })
        }
    })
</script>
</body>
</html>